import Storage from 'web-storage-cache'//WebStorageCache 对HTML5 localStorage 和sessionStorage 进行了扩展，添加了超时时间，序列化方法。可以直接存储json对象，同时可以非常简单的进行超时时间的设置。
const localStorage = new Storage() //新建一个本地存储

//设置
export function setLocalStorage(key,value) {
    return localStorage.set(key,value)
}
//获取
export function getLocalStorage(key) {
    return localStorage.get(key)
}

//移除
export function removeLocalStorage(key) {
    return localStorage.delete(key)
}

//全部清空
export function clearLocalStorage() {
    return localStorage.clear()
}
//为每本电子书各自开一个LocalStorage空间
export function setBookObject(fileName,key,value) { 
    let book = getLocalStorage(`${fileName}-info`) //获取书籍已经保存的信息
    if(!book) {  //如果没有书籍信息，让书籍信息为空对象
        book = {} 
    }
    book[key] = value //在已经保存信息的基础上再增加一个键值对
    setLocalStorage(`${fileName}-info`,book) //保存书籍信息
}
//获取某本书信息
export function getBookObject(fileName,key) { 
    let book = getLocalStorage(`${fileName}-info`)
    if(book) { //有书籍信息，返回book[key]
        return book[key]
    } else { 
        return null //没有返回空
    }
}
export function saveFontSize(fileName,fontSize) { 
    setBookObject(fileName,'fontSize',fontSize)
}

export function getFontSize(fileName) { 
    return getBookObject(fileName,'fontSize') //记得加return
}

export function getLocale() {
    return getLocalStorage('locale')
}

export function saveLocale(lacale) { 
    return setLocalStorage('locale',lacale)
}

export function saveFontFamily(fileName,font) { 
    return setBookObject(fileName,'fontFamily',font)
}

export function getFontFamily(fileName) { 
    return getBookObject(fileName,'fontFamily')
}

export function getTheme(fileName) { 
    return getBookObject(fileName,'theme')
}

export function saveTheme(fileName,theme) { 
    return setBookObject(fileName,'theme',theme)
}

export function getLocation(fileName) { //获取阅读进度
    return getBookObject(fileName,'location')
}

export function saveLocation(fileName,location) { //保存阅读进度
    return setBookObject(fileName,'location',location)
}

export function getReadTime(fileName) { //获取阅读时间
    return getBookObject(fileName,'readTime')
}

export function saveReadTime(fileName,readtime) {  //保存阅读时间
    return setBookObject(fileName,'readTime',readtime)
}